using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.CMS;
using Deepcode.Flux.Core.Systems.CMS.TemplateGallery;

public partial class Private_CMS_main_actions_history_viewHistory : Deepcode.Flux.Core.UI.CMS.CMSActionPane
{
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			ContentManager cms = new ContentManager();
			ContentObject [] versions = cms.AdxGetAllVersionsFor( this.ContentID );
			_data.BindJavascript("btnRollback", "onclick", "return confirmRollback();");
			_data.DataSource = versions;
			_data.DataBind();
		}
	}
	#endregion
	#region Page_Init Event
	/// <summary>
	/// Page initialisation
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Init(object sender, EventArgs e)
	{
		_data.ItemCommand += new DataGridCommandEventHandler(_data_ItemCommand);
		_data.ItemDataBound += new DataGridItemEventHandler(_data_ItemDataBound);
	}
	/// <summary>
	/// Item is being bound to the grid, need to get the 
	/// preview button and add it's appropriate version number.
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void _data_ItemDataBound(object sender, DataGridItemEventArgs e)
	{
		LinkButton btnPreview = (LinkButton) e.Item.FindControl("btnPreview");
		if( btnPreview == null ) return;

		ContentObject o = (ContentObject) e.Item.DataItem;
		if( o == null ) return;
		
		btnPreview.Attributes.Add(
				"onclick", 
				String.Format("return doPreview('{0}')", o.VersionNumber));
	}
	#endregion
	#region Interactions
	/// <summary>
	/// Cancel clicked - go back to content view listing
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	/// <summary>
	/// Command is being executed on the grid
	/// </summary>
	/// <param name="source"></param>
	/// <param name="e"></param>
	void _data_ItemCommand(object source, DataGridCommandEventArgs e)
	{
		if( e.Item.ItemType == ListItemType.Footer ||
			e.Item.ItemType == ListItemType.Header ||
			e.Item.ItemType == ListItemType.Pager ||
			e.Item.ItemType == ListItemType.Separator ) return;

		// Get the key of the row being actioned 
		int rowKey = (int) _data.DataKeys[e.Item.ItemIndex];

		// PROCESS COMMANDS
		if (e.CommandName == "Rollback")
		{
			ContentManager cms = new ContentManager();
			ContentObject obj = cms.AdxGetByID( ContentID );
			if (obj != null)
			{
				cms.RollbackTo(ContentID, rowKey, CurrentUser.PK_ID);

				// Audit
				Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseInformation(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
					"Flux",
					"Content Manager",
					"rolled back {0} to version {1}", obj.NodeTitle, rowKey);
			}
			GoBack(true);
		}
	}
	#endregion
	#region Custom Data Binders
	/// <summary>
	/// Returns the URL of the history preview form based on the type
	/// </summary>
	/// <returns></returns>
	protected string GetTypeHistoryForm()
	{
		ContentManager cms = new ContentManager(true);
		ContentObject o = cms.AdxGetByID( this.ContentID );
		string TypeForm = "";
		
		// Get form based on type
		if (o != null)
			if( o.TypeTemplate != null )
				TypeForm = o.TypeTemplate.HistoryForm;

		// If it doesn't have one, default to standard
		if( TypeForm == "" ) 
			TypeForm = "~/admin/private/cms/main/actions/history/preview.aspx";

		// Add parameters
		string url = String.Format("{0}{1}&ContentID={2}&VersionID=",
			TypeForm,
			TypeForm.Contains("?") ? "" : "?",
			ContentID);

		return Page.ResolveClientUrl( url );
	}
	#endregion
}
